/*
 * Copyright 2011 Petri Kainulainen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kainulainen.wickethttps.login;

import net.kainulainen.wickethttps.HomePage;
import net.kainulainen.wickethttps.WicketHttpsSession;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

/**
 * @author Petri Kainulainen
 */
public class LoginForm extends StatelessForm<LoginDTO> {

    private static final String MESSAGE_KEY_LOGIN_FAILED = "error.login.failed";

    private static final String WICKET_ID_FEEDBACK = "feedback";
    private static final String WICKET_ID_USERNAME = "username";
    private static final String WICKET_ID_PASSWORD = "password";

    public LoginForm(String id) {
        super(id, new CompoundPropertyModel(new LoginDTO()));
        init();
    }

    public LoginForm(String id, IModel iModel) {
        super(id, iModel);
        init();
    }

    private void init() {
        add(new FeedbackPanel(WICKET_ID_FEEDBACK));
        add(new TextField(WICKET_ID_USERNAME)
                .setRequired(true)
        );
        add(new PasswordTextField(WICKET_ID_PASSWORD)
                .setRequired(true)
        );
    }

    @Override
    protected void onSubmit() {
        LoginDTO credentials = this.getModelObject();

        String username = credentials.getUsername();
        String password = credentials.getPassword();

        if (isValidUser(username, password)) {
            processSuccessfulLogin(username);
        } else {
            processFailedLogin();
        }
    }

    private boolean isValidUser(String username, String password) {
        if (username.equals("foo") && (password.equals("bar"))) {
            return true;
        }
        return false;
    }

    private void processSuccessfulLogin(String username) {
        WicketHttpsSession currentSession = WicketHttpsSession.get();
        //Invalidates the current session and creates a new secure session.
        //The created secure session cannot be accessed when using http
        //protocol. This option should be used when only https protocol is
        //used after the user has logged in.
        //currentSession.replaceSession();
        currentSession.setAuthenticatedUsername(username);
        forwardUserToHomePage();
    }

    private void forwardUserToHomePage() {
        this.setResponsePage(HomePage.class);
    }

    private void processFailedLogin() {
        error(getString(MESSAGE_KEY_LOGIN_FAILED));
    }
}
